from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.template.loader import get_template
from datetime import datetime
from my_model.models import Test, Post


# Create your views here.
def test(request):
    t = Test(1, "X")
    t.save()
    return render(request, 'index.html')


def homepage(request):
    template = get_template('index.html')
    posts = Post.objects.all()
    now = datetime.now()
    html = template.render(locals())
    return HttpResponse(html)


def showpost(request,slug):
    template = get_template('post.html')
    try:
        post = Post.objects.get(slug=slug)
        if post != None:
            html = template.render(locals())
            return HttpResponse(html)
    except:
        return redirect('/homepage')